package com.dandan.tzx.router;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.net.Uri;
import android.os.Parcelable;
import android.support.annotation.Nullable;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.callback.NavigationCallback;
import com.alibaba.android.arouter.launcher.ARouter;
import com.dandan.tzx.base.R;

import java.io.Serializable;
import java.util.Map;

/**
 * Created by tanzhenxing
 * Date: 2017/12/22 下午2:59
 * Description:
 */
public class DDRouter {
    private volatile static boolean firstExe = false;
    private ARouter mRouter;
    private Postcard postcard;
    private PathReplaceServiceImpl pathReplaceService;
    public static class SingletonHolder {
        public static final DDRouter INSTANCE = new DDRouter();
    }

    public DDRouter() {
    }


    public static final DDRouter getInstance() {
        return SingletonHolder.INSTANCE;
    }

    public void init(Application mApplication, boolean isDebug) {
        if (isDebug) {           // 这两行必须写在init之前，否则这些配置在init过程中将无效
            ARouter.openLog();     // 打印日志
            ARouter.openDebug();   // 开启调试模式(如果在InstantRun模式下运行，必须开启调试模式！线上版本需要关闭,否则有安全风险)
        }
        ARouter.init(mApplication); // 尽可能早，推荐在Application中初始化
        mRouter = ARouter.getInstance();
        pathReplaceService = (PathReplaceServiceImpl) mRouter.build(PathReplaceServiceImpl.PATH).navigation();
    }

    public void destroy() {
        mRouter.destroy();
    }

    public void inject(Object object) {
        mRouter.inject(object);
    }

    public boolean firstExecuted() {
        return !firstExe;
    }

    public DDRouter build(String path) {
        postcard = mRouter.build(path);
        return this;
    }

    @Deprecated
    public DDRouter build(String path, String group) {
        postcard = mRouter.build(path, group);
        return this;
    }

    public DDRouter build(Uri uri) {
        postcard = mRouter.build(uri);
        return this;
    }

    public DDRouter withString(String key, String value) {
        if (postcard != null) {
            postcard.withString(key, value);
        }
        return this;
    }

    public DDRouter withBoolean(String key, boolean value) {
        if (postcard != null) {
            postcard.withBoolean(key, value);
        }
        return this;
    }

    public DDRouter withInt(String key, int value) {
        if (postcard != null) {
            postcard.withInt(key, value);
        }
        return this;
    }

    public DDRouter withObject(@Nullable String key, @Nullable Object value) {
        if (postcard != null) {
            postcard.withObject(key, value);
        }
        return this;
    }

    public DDRouter withParcelable(String key, Parcelable value) {
        if (postcard != null) {
            postcard.withParcelable(key, value);
        }
        return this;
    }

    public DDRouter withParcelableArray(String key, Parcelable[] value) {
        if (postcard != null) {
            postcard.withParcelableArray(key, value);
        }
        return this;
    }

    public DDRouter withSerializable(String key, Serializable value) {
        if (postcard != null) {
            postcard.withSerializable(key, value);
        }
        return this;
    }

    public DDRouter withTransition(int enterAnim, int exitAnim) {
        if (postcard != null) {
            postcard.withTransition(enterAnim, exitAnim);
        }
        return this;
    }

    public Object navigation() {
        if (postcard != null) {
            firstExe = true;
            if (postcard.getEnterAnim() == 0 && postcard.getExitAnim() == 0) {
                postcard.withTransition(R.anim.slide_in_bottom, R.anim.slide_out_bottom);
            }
            return postcard.navigation();
        }
        throw new RuntimeException("must call build");
    }

    public Object navigation(Context context) {
        if (postcard != null) {
            firstExe = true;
            if (postcard.getEnterAnim() == 0 && postcard.getExitAnim() == 0) {
                postcard.withTransition(R.anim.slide_in_bottom, R.anim.slide_out_bottom);
            }
            return postcard.navigation(context);
        }
        throw new RuntimeException("must call build");
    }

    public Object navigation(Context context, NavigationCallback callback) {
        if (postcard != null) {
            firstExe = true;
            if (postcard.getEnterAnim() == 0 && postcard.getExitAnim() == 0) {
                postcard.withTransition(R.anim.slide_in_bottom, R.anim.slide_out_bottom);
            }
            return postcard.navigation(context, callback);
        }
        throw new RuntimeException("must call build");
    }


    public void navigation(Activity activity, int requestCode) {
        if (postcard != null) {
            firstExe = true;
            if (postcard.getEnterAnim() == 0 && postcard.getExitAnim() == 0) {
                postcard.withTransition(R.anim.slide_in_bottom, R.anim.slide_out_bottom);
            }
            postcard.navigation(activity, requestCode);
        }
        throw new RuntimeException("must call build");
    }


    public void navigation(Activity activity, int requestCode, NavigationCallback callback) {
        if (postcard != null) {
            firstExe = true;
            if (postcard.getEnterAnim() == 0 && postcard.getExitAnim() == 0) {
                postcard.withTransition(R.anim.slide_in_bottom, R.anim.slide_out_bottom);
            }
            postcard.navigation(activity, requestCode, callback);
        }
        throw new RuntimeException("must call build");
    }



    public <T> T navigation(Class<? extends T> service) {
        return mRouter.navigation(service);
    }


    public void replacePath(String sourcePath, String targetPath) {
        pathReplaceService.replacePath(sourcePath, targetPath);
    }

    public Map<String, String> getReplacePathMap() {
        return pathReplaceService.getReplacePathMap();
    }
}
